package oocl.eshop.ui.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * Servlet Filter implementation class LoginFilter
 */
public class LoginFilter implements Filter {
	 private static String UNAME_REGEX="^1[3|4|5|7|8]\\d{9}$";
	 
    public LoginFilter() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
		System.out.println("login filter boom....");
	}

	/** 
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		// place your code here
		String uname=request.getParameter("mTel");
		String pswd=request.getParameter("mPassword");
//		System.out.println("filter...."+pswd);
		if (uname.isEmpty()) {
			request.setAttribute("warningMsg0", "请填写用户名！！");
			request.getRequestDispatcher("index.jsp").forward(request, response);
		}else if (!uname.matches(UNAME_REGEX)) {
			request.setAttribute("warningMsg1", "用户名错误！！");
			request.getRequestDispatcher("index.jsp").forward(request, response);
		} else if (pswd.isEmpty()) {
			request.setAttribute("warningMsg2", "请填写密码！！");
			request.getRequestDispatcher("index.jsp").forward(request, response);
		}else {
			chain.doFilter(request, response);
		}
		// pass the request along the filter chain
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
		System.out.println("login filter init....");
	}

}
